const { ccclass, property } = cc._decorator;

import o from "./frameworkManager"
import i from "./EngineUtil"
import r from "./MatchGameConfig"
import n, { PropToOtherType } from "./MatchGameDataMgr"
import l from "./ObstacleCtrl"
import s from "./MatchGameUtilsDataMgr"
@ccclass
class MatchGameGuideDataMgr {

    public moveGuideSquares = [];
    public lightGuideSquares = [];
    public propToPropGuideArr = [];
    public propToAnyGuideArr = [];
    public propToPropStep = 0;
    public matchPropGuideArr = [];
    public matchMatchGuideArr = [];
    public matchPropStep = 0;
    public matchMatchStep = 0;
    constructor() {

    }

    static _instance;
    static _getInstance() {
        this._instance || (MatchGameGuideDataMgr._instance = new MatchGameGuideDataMgr());
        return MatchGameGuideDataMgr._instance;
    };
    public getAllMatchsGuideArr() {
        for (var e = [], t = [], a = [], o = 0; o < n._map_row; o++) for (var i = 0; i < n._map_col; i++) if (n.middleSquareGrid[o] && n.middleSquareGrid[o][i]) {
            var r = this.getMatchItemGuide(n.middleSquareGrid[o][i]);
            r && r.length && (e = e.concat(r));
        }
        for (var s = e.length - 1; s >= 0; s--) {
            var c = e[s];
            if (c) {
                var u = c.itemPos, p = c.moveMatch;
                (l.getIsHoney(u.row, u.col) || l.getIsHoney(p.row, p.col)) && e.splice(s, 1);
            }
        }
        e.sort(function (e, t) {
            return t.propType - e.propType;
        });
        e.forEach( (e) => {
            e && e.propType > 0 && t.push(e);
            e && e.propType < 0 && a.push(e);
        });
        this.matchPropGuideArr = t;
        this.matchMatchGuideArr = a;
    };
    public setGuideNormal() {
        this.moveGuideSquares && this.moveGuideSquares.length && this.moveGuideSquares.forEach( (e) => {
            e && e.item && e.item.setGuideNormal();
        });
        this.lightGuideSquares && this.lightGuideSquares.length && this.lightGuideSquares.forEach( (e) => {
            e && e.setGuideNormal();
        });
        this.moveGuideSquares = [];
        this.lightGuideSquares = [];
    };
    public guideProps(e) {
        var t = e.pos1, a = e.pos2, o = (e.type, e.dire1), i = e.dire2;
        this.moveGuideSquares = [];
        this.lightGuideSquares = [];
        this.moveGuideSquares.push({
            item: n.middleSquareGrid[t.row][t.col],
            dire: o
        });
        this.moveGuideSquares.push({
            item: n.middleSquareGrid[a.row][a.col],
            dire: i
        });
        var r = [];
        r.push({
            row: t.row,
            col: t.col
        });
        r.push({
            row: a.row,
            col: a.col
        });
        n.playGuideLineAni(r);
        n.playGuideAani();
    };
    public guideMatchsToProp(e) {
        var t = this, a = e.sameArr, o = e.moveMatch, i = e.direction, r = (e.propType,
            e.itemPos);
        this.moveGuideSquares = [];
        this.lightGuideSquares = [];
        var l = [];
        a.forEach( (e) => {
            l.push({
                row: e.row,
                col: e.col
            });
        });
        l.push({
            row: r.row,
            col: r.col
        });
        n.playGuideLineAni(l);
        this.moveGuideSquares.push({
            item: n.middleSquareGrid[o.row][o.col],
            dire: i
        });
        a.forEach( (e) => {
            t.lightGuideSquares.push(n.middleSquareGrid[e.row][e.col]);
        });
        n.playGuideAani();
    };
    public guideMatchToMatch(e) {
        var t = this, a = e.sameArr, o = e.moveMatch, i = e.direction;
        e.propType;
        this.moveGuideSquares = [];
        this.lightGuideSquares = [];
        this.moveGuideSquares.push({
            item: n.middleSquareGrid[o.row][o.col],
            dire: i
        });
        a.forEach( (e) => {
            t.lightGuideSquares.push(n.middleSquareGrid[e.row][e.col]);
        });
        n.playGuideAani();
    };
    public getAllPropGuide() {
        var e = this;
        this.propToPropGuideArr = [];
        this.propToAnyGuideArr = [];
        for (var t = [], a = 0; a < n._map_row; a++) for (var o = 0; o < n._map_col; o++) {
            var i = this.getPropGuide(a, o);
            i && t.push(i);
        }
        t.forEach(function (t) {
            t.type == PropToOtherType.Prop ? e.propToPropGuideArr.push(t) : e.propToAnyGuideArr.push(t);
        });
    };
    public getPropGuide(e, t) {
        var a = {
            pos1: null,
            pos2: null,
            type: -1,
            dire1: -1,
            dire2: -1
        };
        if (s.getIsPropItem(e, t)) {
            a.pos1 = {
                row: e,
                col: t
            };
            var o = s.getHalfNeighborSquareByType(e, t, r.SquareType.PropItem);
            if (o.length) {
                var l = o[i.getRandomNum(0, o.length - 1)];
                a.pos2 = {
                    row: l._row,
                    col: l._col
                };
                a.type = PropToOtherType.Prop;
                a.dire1 = s.getSquaresDirection(a.pos1, a.pos2);
                a.dire2 = s.getSquaresDirection(a.pos2, a.pos1);
                return a;
            }
            a.type = PropToOtherType.Other;
            var c = s.getNeighborSquareByType(e, t, r.SquareType.MatchItem);
            if (c.length) {
                l = c[i.getRandomNum(0, c.length - 1)];
                a.pos2 = {
                    row: l._row,
                    col: l._col
                };
                a.type = PropToOtherType.Match;
                a.dire1 = s.getSquaresDirection(a.pos1, a.pos2);
                a.dire2 = s.getSquaresDirection(a.pos2, a.pos1);
            }
            return a;
        }
        return null;
    };
    public getMatchItemGuide(e) {
        if (e && cc.isValid(e) && e.canMove && !n.isHaveObstacleInHigherLayout(e)) {
            for (var t = [], a = [], i = 0; i <= r.MAX_MATCH_TYPE_COUNT; i++) a[i] = 0;
            var l = null, c = [];
            t[0] = null;
            for (var u = e._col - 1; u >= 0 && ((C = n.middleSquareGrid[e._row][u]) && C.squareType === r.SquareType.MatchItem && C.itemType != r.MatchItemType.null && !n.isHaveObstacleInHigherLayout(C)); u--) if (u != e._col - 1) {
                if (C.itemType != l) break;
                c.push(C);
            } else {
                l = C.itemType;
                c.push(C);
            }
            if (l && c.length) {
                var p = this.getIsSameMatchItem(e._row - 1, e._col + 1, l);
                t[0] = p ? {
                    id: l,
                    arr: c,
                    dire: 0,
                    pro: p
                } : {
                    id: l,
                    arr: c,
                    dire: 0
                };
                a[l] += c.length;
            }
            var d = null, h = [];
            t[1] = null;
            for (var f = e._row + 1; f < n._map_row && n.middleSquareGrid[f] && n.middleSquareGrid[f][e._col] && !n.isHaveObstacleInHigherLayout(n.middleSquareGrid[f][e._col]) && ((C = n.middleSquareGrid[f][e._col]) && C.squareType === r.SquareType.MatchItem && C.itemType != r.MatchItemType.null); f++) if (f != e._row + 1) {
                if (C.itemType != d) break;
                h.push(C);
            } else {
                d = C.itemType;
                h.push(C);
            }
            if (d && h.length) {
                p = this.getIsSameMatchItem(e._row + 1, e._col + 1, d);
                t[1] = p ? {
                    id: d,
                    arr: h,
                    dire: 1,
                    pro: p
                } : {
                    id: d,
                    arr: h,
                    dire: 1
                };
                a[d] += h.length;
            }
            var g = null, y = [];
            t[2] = null;
            for (var m = e._col + 1; m < n._map_col && ((C = n.middleSquareGrid[e._row][m]) && C.squareType === r.SquareType.MatchItem && !n.isHaveObstacleInHigherLayout(C)); m++) if (m != e._col + 1) {
                if (C.itemType != g) break;
                y.push(C);
            } else {
                g = C.itemType;
                y.push(C);
            }
            if (g && y.length) {
                p = this.getIsSameMatchItem(e._row - 1, e._col + 1, g);
                t[2] = p ? {
                    id: g,
                    arr: y,
                    dire: 2,
                    pro: p
                } : {
                    id: g,
                    arr: y,
                    dire: 2
                };
                a[g] += y.length;
            }
            var _ = null, v = [];
            t[3] = null;
            for (var b = e._row - 1; b >= 0 && n.middleSquareGrid[b] && n.middleSquareGrid[b][e._col] && !n.isHaveObstacleInHigherLayout(n.middleSquareGrid[b][e._col]); b--) {
                var C;
                if (!(C = n.middleSquareGrid[b][e._col]) || C.squareType !== r.SquareType.MatchItem) break;
                if (b != e._row - 1) {
                    if (C.itemType != _) break;
                    v.push(C);
                } else {
                    _ = C.itemType;
                    v.push(C);
                }
            }
            if (_ && v.length) {
                p = this.getIsSameMatchItem(e._row - 1, e._col - 1, _);
                t[3] = p ? {
                    id: _,
                    arr: v,
                    dire: 3,
                    pro: p
                } : {
                    id: _,
                    arr: v,
                    dire: 3
                };
                a[_] += v.length;
            }
            for (var T = 0, M = [], P = 0, S = [], w = 0, A = [], O = 0; O < t.length; O++) t[O] && t[O].arr && 1 != t[O].arr.length && 2 == t[O].arr.length && P++;
            for (i = 1; i < a.length; i++) if (a[i] > T) {
                T = a[i];
                w = i;
            }
            if (!(T < 2)) {
                var I = [], N = [], E = null, G = -1, k = [];
                for (i = 0; i <= 3; i++) if (t[i]) if (t[i].id !== w) t[i] = null; else {
                    k = k.concat(t[i].arr);
                    A.push(t[i]);
                    1 == t[i].arr.length ? M.push(t[i]) : 2 == t[i].arr.length && S.push(t[i]);
                }
                A.sort(function (e, t) {
                    return e.dire - t.dire;
                });
                M.length;
                P = S.length;
                var D = e._col, B = e._row, L = s.getIsNoHeightSquareMatchItem(B + 1, D - 1), R = s.getIsNoHeightSquareMatchItem(B + 1, D + 1), F = s.getIsNoHeightSquareMatchItem(B - 1, D + 1), x = s.getIsNoHeightSquareMatchItem(B - 1, D - 1), U = s.getIsNoHeightSquareMatchItem(B, D - 1), q = s.getIsNoHeightSquareMatchItem(B + 1, D), H = s.getIsNoHeightSquareMatchItem(B, D + 1), j = s.getIsNoHeightSquareMatchItem(B - 1, D);
                if (A.length >= 3) if (4 == P) {
                    o.log("找到光球十字型");
                    if (Math.random() > .5) {
                        N = (N = N.concat(S[0].arr)).concat(S[2].arr);
                        E = (Math.random() > .5 ? S[1] : S[3]).arr[0];
                        G = r.PropType.LightingBall;
                        I.push({
                            sameArr: N,
                            moveMatch: E,
                            direction: -1,
                            propType: G
                        });
                    } else {
                        N = (N = N.concat(S[1].arr)).concat(S[3].arr);
                        E = (Math.random() > .5 ? S[0] : S[2]).arr[0];
                        G = r.PropType.LightingBall;
                        I.push({
                            sameArr: N,
                            moveMatch: E,
                            direction: -1,
                            propType: G
                        });
                    }
                } else if (3 == P) {
                    if (0 == S[0].dire && 1 == S[1].dire && 2 == S[2].dire) {
                        N = (N = N.concat(S[0].arr)).concat(S[2].arr);
                        E = S[1].arr[0];
                        G = r.PropType.LightingBall;
                        I.push({
                            sameArr: N,
                            moveMatch: E,
                            direction: -1,
                            propType: G
                        });
                    } else if (0 == S[0].dire && 1 == S[1].dire && 3 == S[2].dire) {
                        N = (N = N.concat(S[1].arr)).concat(S[2].arr);
                        E = S[0].arr[0];
                        G = r.PropType.LightingBall;
                        I.push({
                            sameArr: N,
                            moveMatch: E,
                            direction: -1,
                            propType: G
                        });
                    } else if (1 == S[0].dire && 2 == S[1].dire && 3 == S[2].dire) {
                        N = (N = N.concat(S[0].arr)).concat(S[2].arr);
                        E = S[1].arr[0];
                        G = r.PropType.LightingBall;
                        I.push({
                            sameArr: N,
                            moveMatch: E,
                            direction: -1,
                            propType: G
                        });
                    } else if (0 == S[0].dire && 2 == S[1].dire && 3 == S[2].dire) {
                        N = (N = N.concat(S[0].arr)).concat(S[1].arr);
                        E = S[2].arr[0];
                        G = r.PropType.LightingBall;
                        I.push({
                            sameArr: N,
                            moveMatch: E,
                            direction: -1,
                            propType: G
                        });
                    }
                } else if (2 == P && M.length > 0) if (2 == Math.abs(S[1].dire - S[0].dire)) {
                    N = (N = N.concat(S[0].arr)).concat(S[1].arr);
                    E = M[0].arr[0];
                    G = r.PropType.LightingBall;
                    I.push({
                        sameArr: N,
                        moveMatch: E,
                        direction: -1,
                        propType: G
                    });
                } else {
                    N = (N = N.concat(S[0].arr)).concat(S[1].arr);
                    E = M[0].arr[0];
                    G = r.PropType.Tnt;
                    I.push({
                        sameArr: N,
                        moveMatch: E,
                        direction: -1,
                        propType: G
                    });
                } else if (1 == P) {
                    var V = S[0].dire < 2 ? 2 + S[0].dire : S[0].dire - 2, W = null;
                    for (i = 0; i < M.length; i++) M[i].dire == V && (W = M[i]);
                    if (4 == A.length) {
                        if (W) {
                            N = N.concat(S[0].arr);
                            M.forEach( (e) => {
                                e.dire !== V && (N = N.concat(e.arr));
                            });
                            E = W.arr[0];
                            G = r.PropType.Tnt;
                            I.push({
                                sameArr: N,
                                moveMatch: E,
                                direction: -1,
                                propType: G
                            });
                        }
                    } else if (W) {
                        var Y = null;
                        for (i = 0; i < M.length; i++) M[i].dire !== V && M[i].dire !== S[0].dire && (Y = M[i]);
                        if (Y) {
                            N = N.concat(S[0].arr);
                            M.forEach( (e) => {
                                e.dire == V && (N = N.concat(e.arr));
                            });
                            E = Y.arr[0];
                            G = S[0].dire % 2 == 0 ? r.PropType.RocketVertial : r.PropType.RocketHorzontall;
                            I.push({
                                sameArr: N,
                                moveMatch: E,
                                direction: -1,
                                propType: G
                            });
                        }
                    }
                }
                var J = [];
                if (L && U && q && (H || j) && L.itemType == w && U.itemType == w && q.itemType == w && (H && H.itemType == w || j && j.itemType == w)) {
                    E = H && H.itemType == w ? H : j;
                    (J = []).push(L);
                    J.push(U);
                    J.push(q);
                    G = r.PropType.Propeller;
                    I.push({
                        sameArr: J,
                        moveMatch: E,
                        direction: -1,
                        propType: G
                    });
                }
                if (R && q && H && (U || j) && R.itemType == w && q.itemType == w && H.itemType == w && (j && j.itemType == w || U && U.itemType == w)) {
                    (J = []).push(R);
                    J.push(q);
                    J.push(H);
                    E = j && j.itemType == w ? j : U;
                    G = r.PropType.Propeller;
                    I.push({
                        sameArr: J,
                        moveMatch: E,
                        direction: -1,
                        propType: G
                    });
                }
                if (F && H && j && (U || q) && F.itemType == w && H.itemType == w && j.itemType == w && (U && U.itemType == w || q && q.itemType == w)) {
                    (J = []).push(F);
                    J.push(H);
                    J.push(j);
                    E = U && U.itemType == w ? U : q;
                    G = r.PropType.Propeller;
                    I.push({
                        sameArr: J,
                        moveMatch: E,
                        direction: -1,
                        propType: G
                    });
                }
                if (x && U && j && (q || H) && x.itemType == w && U.itemType == w && j.itemType == w && (q && q.itemType == w || H && H.itemType == w)) {
                    (J = []).push(x);
                    J.push(U);
                    J.push(j);
                    E = q && q.itemType == w ? q : H;
                    G = r.PropType.Propeller;
                    I.push({
                        sameArr: J,
                        moveMatch: E,
                        direction: -1,
                        propType: G
                    });
                }
                if (s.isMatchsSame([U, q, H])) {
                    E = q;
                    N = [U, q, H];
                    G = -1;
                    I.push({
                        sameArr: N,
                        moveMatch: E,
                        direction: -1,
                        propType: G
                    });
                } else if (s.isMatchsSame([U, q, j])) {
                    E = U;
                    N = [U, q, j];
                    G = -1;
                    I.push({
                        sameArr: N,
                        moveMatch: E,
                        direction: -1,
                        propType: G
                    });
                } else if (s.isMatchsSame([q, H, j])) {
                    E = H;
                    N = [q, H, j];
                    G = -1;
                    I.push({
                        sameArr: N,
                        moveMatch: E,
                        direction: -1,
                        propType: G
                    });
                } else if (s.isMatchsSame([U, H, j])) {
                    E = j;
                    N = [U, H, j];
                    G = -1;
                    I.push({
                        sameArr: N,
                        moveMatch: E,
                        direction: -1,
                        propType: G
                    });
                }
                if (1 == M.length && 1 == S.length) {
                    N = k;
                    E = M[0].arr[0];
                    G = -1;
                    I.push({
                        sameArr: N,
                        moveMatch: E,
                        direction: -1,
                        propType: G
                    });
                }
                if (2 == S.length) {
                    N = k;
                    E = Math.random() > .5 ? S[0].arr[0] : S[1].arr[0];
                    G = -1;
                    I.push({
                        sameArr: N,
                        moveMatch: E,
                        direction: -1,
                        propType: G
                    });
                }
                if (N.length && E) {
                    var z = [];
                    var X = function(t) {
                      var o = I[t];
                      var a;
                      if (o.moveMatch._row == e._row) {
                        a = e._col - o.moveMatch._col == 1 ? 0 : 2;
                      } else if (e._row - o.moveMatch._row == 1) {
                        a = 3;
                      } else {
                        a = 1;
                      }
                      
                      o.sameArr.forEach(function(e, t) {
                        if (e == o.moveMatch) {
                          o.sameArr.splice(t, 1);
                        }
                      });
                      
                      var i = {
                        row: o.moveMatch._row,
                        col: o.moveMatch._col
                      };
                      var r = [];
                      
                      o.sameArr.forEach(function(e) {
                        if (e._row !== i.row || e._col !== i.col) {
                          r.push({
                            row: e._row,
                            col: e._col
                          });
                        }
                      });
                      
                      var n = {
                        sameArr: r,
                        moveMatch: i,
                        direction: a,
                        propType: o.propType,
                        itemPos: {
                          row: e._row,
                          col: e._col
                        }
                      };
                      
                      z.push(n);
                    };
                    
                    for (var K = 0; K < I.length; K++) {
                      X(K);
                    }
                  
                    return z;
                  }
                  
            }
        }
    };
    public getIsSameMatchItem(e, t, a) {
        var o = n.getIsSameMatchItem(e, t, a);
        return o && !n.isHaveObstacleInHigherLayout(o) ? o : null;
    };

}

export default MatchGameGuideDataMgr._getInstance()